package cn.slipi.core.filter;

import cn.slipi.common.decorator.PayloadServerWebExchangeDecorator;
import cn.slipi.common.utils.TraceUtil;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SignalType;

public class LogFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
        this.start();
        return webFilterChain.filter(new PayloadServerWebExchangeDecorator(serverWebExchange)).doFinally(this::end);
    }

    private void start() {
        TraceUtil.traceStart(null);
    }

    private void end(SignalType signalType) {
        TraceUtil.traceEnd();
    }

}
